      
package b_and_b_properties;
// View an image in a frame that exactly surrounds it.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

class ImageViewer extends Frame {
    public ImageViewer(Image image){
        setImage(image);
        setup();
    }

        // Obtain the image from the producer.
    public ImageViewer(ImageProducer producer){
        setImage(createImage(producer));
        setup();
    }

        // Perform common frame setup for the two constructors.
    protected void setup(){
        // Dimensions are adjusted once the dimensions of
        // the image are known.
        final int initialWidth = 200, initialHeight = 200;
        setSize(initialWidth,initialHeight);

        // Quit on a keypress.
        addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e){
                setVisible(false);
                Container parent = getParent();
                if(parent != null){
                    parent.remove(ImageViewer.this);
                }
                else{
                    System.exit(0);
                }
            }
        });
        setVisible(true);
    }

    public void paint(Graphics g){
        // Make sure it appears just within the frame's insets.
        Insets insets = getInsets();
        g.drawImage(getImage(),insets.left,insets.top,this);
    }

    // Override Frame.imageUpdate - the implemention of ImageObserver.
    public boolean imageUpdate(Image img,int infoflags,int x, int y,
                                int width, int height){
        // Find out what has changed.
        boolean result = super.imageUpdate(img,infoflags,x,y,width,height);
        // See if the image is complete.
        if((infoflags & ImageObserver.ALLBITS) != 0){
            // Set the frame's size according to the image size.
            Insets insets = getInsets();
            // Include in the width and height the insets space required
            // for the frame's decoration.
            int newWidth = insets.left+width+insets.right;
            int newHeight = insets.top+height+insets.bottom;
            setSize(newWidth,newHeight);
        }
        else if((infoflags & (ImageObserver.ABORT | ImageObserver.ERROR)) != 0){
            System.err.println("Image error.");
        }
        else{
            // Ignore.
        }
        return result;
    }

    protected Image getImage(){
        return image;
    }

    protected void setImage(Image i){
        image = i;
    }    

    private Image image;
}

